# coding=utf-8
__author__ = 'Administrator'

import urllib,http
import json

class CPU(object):

    info = 'info'
    tg = 35.0
    ip = "192.168.0.1"
    json = ""

    @staticmethod
    def to_json(obj):
        d = {}
        d['__class__'] = obj.__class__.__name__
        d['__module__'] = obj.__module__
        d.update(obj.__dict__)
        return d

    @staticmethod
    def dict2object(json):
        print("method dict2object param : %s" % json)
        if'__class__' in json:
            class_name = json.pop('__class__')
            module_name = json.pop('__module__')
            module = __import__(module_name)
            class_ = getattr(module, class_name)
            args = dict((key.encode('ascii'), value) for key, value in d.items())  # get args
            inst = class_(**args)  # create new instance
        else:
            inst = json

        return inst

    def post_info_to_server(self, server, port):
        params = urllib.urlencode(self.to_json())
        headers = {"Content-type": "application/x-www-form-urlencoded",  "Accept": "text/plain"}
        conn = http.HTTPConnection(server+":"+port)
        conn.request("POST", "/cgi-bin/query", params, headers)
        response = conn.getresponse()
        if '200' == response.status:
            print('success')
        data = response.read()
        conn.close()
